package com.zoke.support.view;

import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;

import com.zoke.support.R;
import com.zoke.support.base.BaseActivity;
import com.zoke.support.base.BaseFragment;

/**
 * 封装项目的title 使用这个title处理时必须将app_title.xml布局include到你的主布局中
 * 支持Activity及Fragment中使用 ----可减低耦合性 利用父类来设置title的颜色或者背景色
 **/
public class TitleManager {

	protected BaseActivity mBase;
	protected BaseFragment mBaseFragment;

	public TitleManager(BaseActivity baseAct) {
		this.mBase = baseAct;
	}

	public TitleManager(BaseFragment baseFragment) {
		this.mBaseFragment = baseFragment;
	}

	/** 获取左侧按钮引用 **/
	public ZokeButton getTitleLeft() {
		// 如果activity==null 则是fragment
		return mBase == null ? (ZokeButton) mBaseFragment
				.findViewById(R.id._title_left) : (ZokeButton) mBase
				.findViewById(R.id._title_left);
	}

	/** 获取app标题容器 **/
	public RelativeLayout getAppTitleContainer() {
		return mBase == null ? (RelativeLayout) mBaseFragment
				.findViewById(R.id._titlecontainer_rl) : (RelativeLayout) mBase
				.findViewById(R.id._titlecontainer_rl);
	}

	/** 设置标题的背景色 **/
	public TitleManager setBackgroundResource(int resid) {
		RelativeLayout mContainer = getAppTitleContainer();
		mContainer.setBackgroundResource(resid);
		return this;
	}

	/** 获取左侧按钮的父容器 **/
	public ZokeRelativeLayout getTitleLeftContainer() {
		// 如果activity==null 则是fragment
		return mBase == null ? (ZokeRelativeLayout) mBaseFragment
				.findViewById(R.id._title_left_container)
				: (ZokeRelativeLayout) mBase
						.findViewById(R.id._title_left_container);
	}

	/** 设置左侧按钮图标 **/
	public TitleManager setLeftIcon(int resid) {
		ZokeRelativeLayout titleBtnCon = getTitleLeftContainer();
		ZokeButton titleBtn = getTitleLeft();
		// 如果是FragMent需要设置点击事件
		if (null != mBaseFragment)
			titleBtnCon.setOnClickListener(mBaseFragment);
		// 设置图标的时候设置显示
		titleBtnCon.setVisibility(View.VISIBLE);
		titleBtn.setBackgroundResource(resid);
		return this;
	}

	/** 隐藏按钮 **/
	public TitleManager hideLeft() {
		getTitleLeftContainer().setVisibility(View.INVISIBLE);
		return this;
	}

	/** 获取title引用 **/
	public ZokeButton getTitle() {
		return mBase == null ? (ZokeButton) mBaseFragment
				.findViewById(R.id._title) : (ZokeButton) mBase
				.findViewById(R.id._title);
	}

	/** 获取title的父容器 **/
	public ZokeRelativeLayout getTitleContainer() {
		return mBase == null ? (ZokeRelativeLayout) mBaseFragment
				.findViewById(R.id._title_container)
				: (ZokeRelativeLayout) mBase
						.findViewById(R.id._title_container);
	}

	/** 设置标题内容 **/
	public TitleManager setTitle(CharSequence text) {
		ZokeRelativeLayout titleBtnContainer = getTitleContainer();
		// 先显示在设置
		ZokeButton titleBtn = getTitle();
		// 如果是FragMent需要设置点击事件
		if (null != mBaseFragment)
			titleBtnContainer.setOnClickListener(mBaseFragment);
		titleBtnContainer.setVisibility(View.VISIBLE);
		titleBtn.setText(text);
		return this;
	}

	/** 设置标题的背景 **/
	public TitleManager setTitleIcon(int resid) {
		ZokeRelativeLayout titleBtnContainer = getTitleContainer();
		// 先显示再设置
		ZokeButton titleBtn = getTitle();
		// 如果是FragMent需要设置点击事件
		if (null != mBaseFragment)
			titleBtnContainer.setOnClickListener(mBaseFragment);
		titleBtnContainer.setVisibility(View.VISIBLE);
		titleBtn.setBackgroundResource(resid);
		return this;
	}

	/** 隐藏title标题 **/
	public TitleManager hideTitle() {
		getTitleContainer().setVisibility(View.INVISIBLE);
		return this;
	}

	/** 获取右侧按钮引用 **/
	public ZokeButton getTitleRight() {
		return mBase == null ? (ZokeButton) mBaseFragment
				.findViewById(R.id._title_right) : (ZokeButton) mBase
				.findViewById(R.id._title_right);
	}

	/** 获取右侧按钮的父容器 **/
	public ZokeRelativeLayout getTitleRightContainer() {
		return mBase == null ? (ZokeRelativeLayout) mBaseFragment
				.findViewById(R.id._title_right_container)
				: (ZokeRelativeLayout) mBase
						.findViewById(R.id._title_right_container);
	}

	/** 设置右侧按钮图标 **/
	public TitleManager setRightIcon(int resid) {
		ZokeRelativeLayout titleBtnContainer = getTitleRightContainer();
		// 先显示再设置
		ZokeButton titleBtn = getTitleRight();
		// 如果是FragMent需要设置点击事件
		if (null != mBaseFragment)
			titleBtnContainer.setOnClickListener(mBaseFragment);
		titleBtnContainer.setVisibility(View.VISIBLE);
		titleBtn.setBackgroundResource(resid);
		return this;
	}

	public TitleManager setRightText(CharSequence text) {
		ZokeRelativeLayout titleBtnContainer = getTitleRightContainer();
		// 先显示再设置
		ZokeButton titleBtn = getTitleRight();
		// 如果是FragMent需要设置点击事件
		if (null != mBaseFragment)
			titleBtnContainer.setOnClickListener(mBaseFragment);
		titleBtnContainer.setVisibility(View.VISIBLE);
		LayoutParams params = titleBtn.getLayoutParams();
		params.width = LayoutParams.WRAP_CONTENT;
		params.height = LayoutParams.WRAP_CONTENT;
		titleBtn.setText(text);
		return this;
	}

	/** 设置左侧按钮为文字 **/
	public TitleManager setLeftText(CharSequence text) {
		ZokeRelativeLayout titleBtnContainer = getTitleLeftContainer();
		// 先显示再设置
		ZokeButton titleBtn = getTitleLeft();
		// 如果是FragMent需要设置点击事件
		if (null != mBaseFragment)
			titleBtnContainer.setOnClickListener(mBaseFragment);
		titleBtnContainer.setVisibility(View.VISIBLE);
		LayoutParams params = titleBtn.getLayoutParams();
		params.width = LayoutParams.WRAP_CONTENT;
		params.height = LayoutParams.WRAP_CONTENT;
		titleBtn.setText(text);
		return this;
	}

	/** 隐藏右侧按钮 **/
	public TitleManager hideRight() {
		getTitleRightContainer().setVisibility(View.INVISIBLE);
		return this;
	}
}
